package top.wineye.spring.bean.instantiation;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.wineye.spring.bean.factory.UserFactory;

public class BeanInstantiationDemo {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:META-INF/bean-instantiation-context.xml");

        AutowireCapableBeanFactory beanFactory = context.getBeanFactory();

        // 1. 通过静态工厂方法
        System.out.println("instantiate by factory method: "
                + context.getBean("user"));

        // 2. 通过实例的工厂方法
        System.out.println(context.getBean("userByInstanceFactory"));

        // 3. 通过 FactoryBean
        System.out.println(context.getBean("userFactoryBean"));

        // 4. AutowireCapableBeanFactory#createBean
        UserFactory factory = beanFactory.createBean(UserFactory.class);
        System.out.println(factory.build());

    }
}
